package gold.digger;

import gold.utils.InputUtil;

/**
 * Created by fanzhenyu02 on 2020/6/27.
 * common problem solver template.
 */
public class LC153 {
    public long startExecuteTime = System.currentTimeMillis();


    class Solution {
        public int findMin(int[] arr) {
            int left = 0, right = arr.length - 1, mid;
            while (right - left >= 2 && arr[left] > arr[right]) {
                mid = left + (right - left) / 2;

                if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) return arr[mid + 1];
                if (arr[mid] > arr[left] && arr[mid] > arr[right]) {
                    left = mid;
                } else {
                    right = mid;
                }
            }

            return right - left >= 2 ? arr[left] : Math.min(arr[left], arr[right]);
        }
    }

    public void run() {
        Solution solution = new Solution();
        int[] arr = InputUtil.toIntegerArray("[5,1,2,3,4]");
        System.out.println(solution.findMin(arr));
    }

    public static void main(String[] args) throws Exception {
        LC153 an = new LC153();
        an.run();

        System.out.println("\ncurrent solution total execute time: " + (System.currentTimeMillis() - an.startExecuteTime) + " ms.");
    }
}
